import junit.framework.TestCase;

/* Foundation Layer
 * DataBase Test Cases
 * Written by Devin Shively
 *
 * Expected functions of Database
 * -------------------------------
 * isEmpty();
 * addElement();
 * removeElement();
 * getElementCount();
 * getElement();
 * 
 */

public class DataBaseTest extends TestCase {
	
    private DataBase db;
	private Element elem;
	
    /**
     * Sets up the test fixture.
     *
     * Called before every test case method.
     */
    protected void setUp() {
		
        db = new DataBase();
		
        elem = new Element("string", 0xFF0000, "test.java");
		
        db.addElement(elem);
    }
	
    /**
     * Tears down the test fixture.
     *
     * Called after every test case method.
     */
    protected void tearDown() {
        // release objects under test here, if necessary
    }
	
    /**
     * Tests to see if the database is empty, which returns false.
     */
    public void testEmpty() {
		
        assertEquals(0, db.isEmpty());
    }
	
    /**
     * Tests adding an element to the database.
     */
    public void testAddElement() {
		
        Element elem2 = new Element("string2", 0xFFAB00, "test.java");
        
		db.addElement(elem2);
		
		assertEquals(2, db.getElementCount());
		
        assertEquals(elem2, db.getElement(elem2) );
    }
	
    /**
     * Tests removing an element from the database.
     *
     * @throws ElementNotFoundException If the element was not in the database.
     */
    public void testRemoveItem() throws ElementNotFoundException {
		
        db.removeElement(elem);
		
        assertEquals(0, db.getElementCount());
    }
	
    /**
     * Tests removing an unknown element from the database.
     *
     * This test is successful if the 
     * ProductNotFoundException is raised.
     */
    public void testRemoveElementNotInDataBase() {
		
        try {
			
            Element elem3 = new Element("chicken", 0xFF1212, "test.java");
            db.removeElement(elem3);
			
            fail("Should raise an ElementNotFoundException");
			
        } catch(ElementNotFoundException expected) {
            // successful test
        }
    }
}
